#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides:          avtp_traffic_listener
# Required-Start:    networking
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Open avtp_traffic_listener service
# Description:       Wrapper for starting / stopping GoldVIP avtp_traffic_listener service
#                    Runs when domain 0 boots / shuts down on systems using init.
### END INIT INFO
# Copyright 2021,2023 NXP

SERVER_PATH="/home/root/can-gw/avtp_listener.py"
DEFAULT_LOG_FILE="/tmp/can2eth_fast_path.log"
AUX0_NETIF="aux0sl"

# Service start function
start_service() {
        # Cleanup previous logs on startup
        rm ${1} > /dev/null 2>&1
        python3 ${SERVER_PATH} --interface "${AUX0_NETIF}"  > "${1}" &
}

# Service stop function
stop_service() {
        kill -9 $(ps aux | grep '[a]vtp_listener.py' | awk '{print $2}') > /dev/null 2>&1
        sleep 1
}

case "$1" in
start)
        echo "Starting AVTP listener service"
        start_service "${2:-${DEFAULT_LOG_FILE}}"
        ;;
stop)
        echo "Stopping AVTP listener service"
        stop_service 
        ;;
restart)
        echo "Restarting AVTP listener service"
        stop_service
        start_service "${2:-${DEFAULT_LOG_FILE}}"
        ;;

*)
        echo "Usage: /etc/init.d/avtp_traffic_listener {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
